package com.java.java8.util.functioninterfacedemo;

import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

/**
 * @author liyanhao
 * @Date 2023-03-26 18:37
 */
public class SupplierDemo {

    public static void main(String[] args) {
        testSupplierWirhLambda();
    }

    public static void testSupplierWirhLambda(){
        final Random random = new Random();
        lazyPrint(()->{
            return random.nextInt();
        });
        //延时三秒生成一个随机数
        lazyPrint(()->{
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return random.nextInt();
        });


    }

    public static void lazyPrint(Supplier<Integer> lazyValue) {
        System.out.println(lazyValue.get());
    }
}